package city.spring.service.estimate.impl;

import city.spring.domain.entity.estimate.EstimateCheckEntity;
import city.spring.domain.entity.estimate.EstimateProjectEntity;
import city.spring.modules.ext.entity.EstimateProjectExt;
import city.spring.modules.ext.service.EstimateProjectExtService;
import city.spring.repository.estimate.EstimateCheckRepository;
import city.spring.service.estimate.EstimateCheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class EstimateCheckServiceImpl extends ServiceImpl<EstimateCheckRepository, EstimateCheckEntity> implements EstimateCheckService {

    private EstimateProjectExtService estimateProjectExtService;

    public EstimateCheckServiceImpl(EstimateProjectExtService estimateProjectExtService) {
        this.estimateProjectExtService = estimateProjectExtService;
    }

    @Override
    public void saveEstimateCheck(EstimateCheckEntity entity) {
        boolean save = save(entity);
        if (!save) {
            throw new RuntimeException("保存失败！");
        }
    }

    @Override
    public void updateEstimateCheck(EstimateCheckEntity entity) {
        boolean update = lambdaUpdate()
                .set(EstimateCheckEntity::getEstimateId, entity.getEstimateId())
                .set(EstimateCheckEntity::getEstimateDate, entity.getEstimateDate())
                .set(EstimateCheckEntity::getEstimateReason, entity.getEstimateReason())
                .set(EstimateCheckEntity::getEstimateType, entity.getEstimateType())
                .set(EstimateCheckEntity::getEstimatePerson, entity.getEstimatePerson())
                .set(EstimateCheckEntity::getElderId, entity.getElderId())
                .set(EstimateCheckEntity::getProviderName, entity.getProviderName())
                .set(EstimateCheckEntity::getProviderRelationship, entity.getProviderRelationship())
                .set(EstimateCheckEntity::getContactsName, entity.getContactsName())
                .set(EstimateCheckEntity::getContactsTel, entity.getContactsTel())
                .set(EstimateCheckEntity::getEstimateStandard, entity.getEstimateStandard())
                .set(EstimateCheckEntity::getInitialLevel, entity.getInitialLevel())
                .set(EstimateCheckEntity::getFinalLevel, entity.getFinalLevel())
                .update();
        if (!update) {
            throw new RuntimeException("保存失败！");
        }
    }

    @Override
    public void deleteEstimateCheck(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteEstimateCheck(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public void setEstimateProject(EstimateCheckEntity checkEntity, EstimateProjectEntity projectEntity, String points) {
        EstimateProjectExt estimateProjectExt = new EstimateProjectExt(null, checkEntity.getId(), projectEntity.getId(), points);
        estimateProjectExtService.save(estimateProjectExt);
    }

}
